//
//  YXPlayerCacheWorker.swift
//  swift_wobo
//
//  Created by yuxin on 2018/12/2.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit

class YXPlayerCacheWorker: NSObject {
    
    
    private var actionManger: YXPlayerActionManger
    
    private var readFileHandle: FileHandle?
    private var writeFileHandle: FileHandle?
    private var filePath: String
    private var currentOffset: UInt64?
    private var starWriteDate: NSDate?
    private var writeBytes: Double = 0.0
    private var isWritting: Bool = false
    
    private let writeFileQueue = DispatchQueue(label: "com.YXPlayer.cacheWriteFileQueue")
    private let kPackageLength = 204800 //200 * 1024
    
    
    
    init(url: URL) {
        let path = YXPlayerCacheFileManger.cacheFilePath(url: url)
        filePath = path
        
        actionManger = YXPlayerActionManger(url: url)
        super.init()
        
    }

    ///根据range 生成actions
    func cachedDataActions(forRange range:NSRange) -> [YXPlayerAction] {
        var actions = [YXPlayerAction]()
        if range.location == NSNotFound {
            return actions
        }
        
        let endOffset = range.location + range.length
        let cachedFragments = actionManger.cacheSegments
        for item in cachedFragments {
            let segmentRange = item.rangeValue
            //交叉重合部分
            let intersctionRange = NSIntersectionRange(range, segmentRange)
            if intersctionRange.length > 0 {
                
                let packageCount = intersctionRange.length/kPackageLength
                for i in 0...packageCount {
                    
                    let offset = i * kPackageLength
                    let offsetLocation = intersctionRange.location + offset
                    let maxLocation = intersctionRange.location + intersctionRange.length
                    let length = (offsetLocation + kPackageLength) > maxLocation ? (maxLocation - offsetLocation) : kPackageLength
                    let ra = NSMakeRange(offsetLocation, length)
                    let action = YXPlayerAction(type: .local, range: ra)
                    actions.append(action)
                }
            }else if segmentRange.location >= endOffset {
                break
            }
        }
        
        if actions.count == 0 {
            let action = YXPlayerAction(type: .remote, range: range)
            actions.append(action)
        }else{
            // Add remote fragments
            var remoteActions = [YXPlayerAction]()
            for (index, item) in actions.enumerated() {
                let actionRange = item.range
                if index == 0 {
                    if range.location < actionRange.location {
                        let ra = NSMakeRange(range.location, actionRange.location - range.location)
                        let action = YXPlayerAction(type: .remote, range: ra)
                        remoteActions.append(action)
                    }
                    remoteActions.append(item)
                }else {
                    if let lastAction = remoteActions.last {
                        let lastEndOffset = lastAction.range.location + lastAction.range.length
                        if actionRange.location > lastEndOffset {
                            let ra = NSMakeRange(lastEndOffset, actionRange.location - lastEndOffset)
                            let action = YXPlayerAction(type: .remote, range: ra)
                            remoteActions.append(action)
                        }
                    }
                    remoteActions.append(item)
                }
                
                if index == actions.count - 1 {
                    let localEndOffset = actionRange.location + actionRange.length
                    if endOffset > localEndOffset {
                        let ra = NSMakeRange(localEndOffset, endOffset)
                        let action = YXPlayerAction(type: .remote, range: ra)
                        remoteActions.append(action)
                    }
                }
            }
            
            actions = remoteActions
        }
        return actions
    }
    
    ///获取range对应的data
    func getCacheData(forRange range:NSRange) -> Data? {
        
        return nil
    }
    
    ///准备写入数据
    func startWritting() {
        
    }
    
    ///写入data到range
    func setCacheData(forRange range:NSRange, data: Data) -> Error? {
        
    
        return  nil
    }
    
    func save() {
        
    }
    
    func finishWritting() {
        
    }
    
    
}
